package org.jingouzhui.domain.strategy.service.rule.tree.factory.engine.impl;

import lombok.extern.slf4j.Slf4j;
import org.jingouzhui.domain.strategy.model.valboj.RuleLogicCheckTypeVO;
import org.jingouzhui.domain.strategy.model.valboj.RuleTreeNodeLineVO;
import org.jingouzhui.domain.strategy.model.valboj.RuleTreeNodeVO;
import org.jingouzhui.domain.strategy.model.valboj.RuleTreeVO;
import org.jingouzhui.domain.strategy.service.rule.tree.ILogicTreeNode;
import org.jingouzhui.domain.strategy.service.rule.tree.factory.DefaultTreeFactory;
import org.jingouzhui.domain.strategy.service.rule.tree.factory.engine.IDecisionTreeEngine;
import org.jingouzhui.types.exception.AppException;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * @description: 决策树规则引擎
 * @author: jingouzhui
 * @date: 2025/5/1 17:03
 */
@Slf4j
public class DecisionTreeEngine implements IDecisionTreeEngine {

    private final Map<String, ILogicTreeNode> logicTreeNodeMap;

    /**
     * 整个规则树
     */
    private final RuleTreeVO ruleTreeVO;

    public DecisionTreeEngine(Map<String, ILogicTreeNode> logicTreeNodeMap, RuleTreeVO ruleTreeVO) {
        this.logicTreeNodeMap = logicTreeNodeMap;
        this.ruleTreeVO = ruleTreeVO;
    }

    @Override
    public DefaultTreeFactory.StrategyAwardVO process(String userId, Long strategyId, Integer awardId) {
        DefaultTreeFactory.StrategyAwardVO strategyAwardData = null;
        //获取根节点的名称
        String nextNode = ruleTreeVO.getTreeRootRuleNode();
        //获取树的map (存储着节点名称和对应节点的映射)
        Map<String, RuleTreeNodeVO> treeNodeMap = ruleTreeVO.getTreeNodeMap();
        //获取第一个节点的信息(记录了规则key:rule_lock /stock/rule_luck_award)
        RuleTreeNodeVO ruleTreeNodeVO = treeNodeMap.get(nextNode);
        while (nextNode != null && ruleTreeNodeVO!= null) {
            //获取决策节点
            ILogicTreeNode logicTreeNode = logicTreeNodeMap.get(ruleTreeNodeVO.getRuleKey());
            String ruleValue = ruleTreeNodeVO.getRuleValue();
            DefaultTreeFactory.TreeActionEntity actionEntity = logicTreeNode.logic(userId, strategyId, awardId,ruleValue);
            strategyAwardData = actionEntity.getStrategyAwardVO();
            RuleLogicCheckTypeVO ruleLogicCheckTypeVO = actionEntity.getRuleLogicCheckTypeVO();
            log.info("决策树引擎【{}】treeId:{} node:{} code:{}", ruleTreeVO.getTreeName(), ruleTreeVO.getTreeId(), nextNode, ruleLogicCheckTypeVO.getCode());

            //获取下个节点名称
            nextNode = nextNode(ruleLogicCheckTypeVO.getCode(), ruleTreeNodeVO.getRuleTreeNodeLineVOList());
            ruleTreeNodeVO = treeNodeMap.get(nextNode);

        }
        return strategyAwardData;
    }
    private String nextNode(String matterValue,  List<RuleTreeNodeLineVO> ruleTreeNodeLineVOList) {
        if (CollectionUtils.isEmpty(ruleTreeNodeLineVOList)) return null;
        for (RuleTreeNodeLineVO ruleTreeNodeLine : ruleTreeNodeLineVOList) {
            if (decisionLogic(matterValue, ruleTreeNodeLine)) {
                return ruleTreeNodeLine.getRuleNodeTo();
            }
        }
        throw  new AppException("配置规则有误");
    }

    private boolean decisionLogic(String matterValue, RuleTreeNodeLineVO nodeLine) {
        switch (nodeLine.getRuleLimitType()) {
            case EQUAL:
                return matterValue.equals(nodeLine.getRuleLimitValue().getCode());
            // 以下规则暂时不需要实现
            case GT:
            case LT:
            case GE:
            case LE:
            default:
                return false;
        }
    }
}
